package com.ybkj.o2o.sales.controller.res.v4;


import com.ybkj.o2o.sales.entity.Coupon;
import com.ybkj.o2o.sales.manager.CouponManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController("CouponResV4")
@RequestMapping(value = "api/coupons/v4")
public class CouponRes {

  @Autowired
  private CouponManager couponManager;


  /**
   * <p>
   * 根据ID查询出优惠券规则信息.
   * </p>
   * 
   * @param id 优惠券id
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "findOne/{id}", method = RequestMethod.GET)
  public @ResponseBody ResultTransport findOne(@PathVariable("id") Long id,
      @RequestParam("appKey") String appKey) {

    if (StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }

    appKey = StringUtils.trimToEmpty(appKey);

    try {

      Coupon coupon = couponManager.findOne(id, appKey);

      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(coupon);

      return instance;

    } catch (Exception exo) {

      exo.printStackTrace();

      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }
  }

  /**
   * 查询线路可使用优惠券（微信公众号）
   * @param passengerId 客户id
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "listValidByPassenger", method = RequestMethod.GET)
  public @ResponseBody ResultTransport listValidByPassenger(
      @RequestParam("passengerId") Long passengerId, @RequestParam(required = false,
          value = "couponType") String couponType, @RequestParam("appKey") String appKey, @RequestParam(required = false, value = "linesId") Long[] linesId) {

    if (null == passengerId || StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }

    appKey = StringUtils.trimToEmpty(appKey);

    try {

      List<Coupon> list = couponManager.listValidByPassenger(passengerId, couponType,null, appKey, linesId);

      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      instance.setData(list);

      return instance;

    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }

  }

  /**
   * 修改优惠券.
   * 
   * @param id 优惠券id
   * @param passengerId 客户id
   * @param passengerPhone 客户手机号
   * @param status 优惠券状态
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "update/{id}", method = RequestMethod.PUT)
  public @ResponseBody ResultTransport update(@PathVariable("id") Long id, @RequestParam(
      required = false, value = "passengerId") Long passengerId, @RequestParam(required = false,
      value = "passengerPhone") String passengerPhone, @RequestParam(required = false,
      value = "status") Integer status,
      @RequestParam(required = true, value = "appKey") String appKey) {

    if (StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }

    try {

      Coupon coupon = couponManager.findOne(id, appKey);

      coupon.setPassengerId(passengerId);
      coupon.setPhone(passengerPhone);
      coupon.setStatus(status);

      couponManager.update(coupon);

      ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      return instance;

    } catch (Exception ex) {
      ex.printStackTrace();
      return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
    }

  }

}
